//
//  KLHouseCell.m
//  KLShoppingList
//
//  Created by apple on 2017/8/19.
//  Copyright © 2017年 NetEaseExam. All rights reserved.
//

#import "KLHouseCell.h"

#import "KLHouseModel.h"

#import "KLConfig.h"

@interface KLHouseCell ()
@property (weak, nonatomic) UIButton *selectedBtn;
@property (weak, nonatomic) UILabel *houseTitleLabel;

@end

@implementation KLHouseCell

+ (instancetype)cellWithTableView:(UITableView *)tableView{
    KLHouseCell *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:house_reuse_id];
    header.bounds = CGRectMake(0, 0, tableView.bounds.size.width, 42);
    if (header == nil) {
//        header = [[[NSBundle mainBundle] loadNibNamed:@"KLHouseCell" owner:nil options:nil] firstObject];
        header = [[KLHouseCell alloc] initWithReuseIdentifier:house_reuse_id];
    }
    return header;
}

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        
        self.contentView.backgroundColor = [UIColor whiteColor];
        
  
        
        UIView *topBorder = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 600, 0.5)];
        topBorder.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:topBorder];
        
        UIView *bottomBorder = [[UIView alloc] initWithFrame:CGRectMake(0, 41.5, 600, 0.5)];
        bottomBorder.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:bottomBorder];
        
        UIButton *selBtn = [[UIButton alloc] init];
        selBtn.frame = CGRectMake(10, 10, 22, 22);
        [selBtn setImage:[UIImage imageNamed:@"circle"] forState:UIControlStateNormal];
        [selBtn setImage:[UIImage imageNamed:@"select"] forState:UIControlStateSelected];
        [selBtn addTarget:self action:@selector(houseSelectedBtnClicked:)  forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:selBtn];
        
        self.selectedBtn = selBtn;
        
        
        UIImageView *iv = [[UIImageView alloc] init];
        iv.frame = CGRectMake(40, 11, 20, 20);
        iv.image = [UIImage imageNamed:@"grocery"];
        [self.contentView addSubview:iv];
        
        UILabel *label = [[UILabel alloc] init];
        label.frame = CGRectMake(68, 12, 600, 17);
        label.textAlignment = NSTextAlignmentLeft;
        label.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:label];
        
        self.houseTitleLabel = label;
        
        
    }
    return self;
}


- (void)setHouseModel:(KLHouseModel *)houseModel{
    _houseModel = houseModel;
    self.houseTitleLabel.text = houseModel.house;
    self.selectedBtn.selected = houseModel.houseIsChoosed;
}

- (void)houseSelectedBtnClicked:(UIButton *)sender {
    sender.selected = !sender.selected;
    
    if (sender.selected == YES) {
        NSLog(@"勾选了house");
    }else{
        NSLog(@"取消勾选了house");
    }

    if (self.delegate && [self.delegate respondsToSelector:@selector(houseSelected:)]) {
        [self.delegate houseSelected:self.sectionIndex];
    }
}



//- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
//    [super setSelected:selected animated:animated];
//
//    // Configure the view for the selected state
//}

@end
